﻿using log4net;

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace JFTest.Nlog
{
    public enum PresetColor
    {
        Yellow,
        Green,
        Red,
        White,
        Black
    }

    /// <summary>
    /// 使用一个FastColoredTextBox <see cref="https://github.com/PavelTorgashov/FastColoredTextBox"/>
    /// 来输出信息，可以使用彩色文本了
    /// 这个控件不支持中文编码，有问题，先不用了
    /// </summary>
    public static class ColoredOutput
    {

        private static ILog _logger = LogManager.GetLogger(typeof(WindowsFormTest.Program));

        private static UIRichTextBox _coloredStatusTextBox;
        private static UIRichTextBox _coloredOperationTextBox;
        //private static readonly TextStyle _higlightStyle = new TextStyle(Brushes.Yellow, null, FontStyle.Regular);
        //private static readonly TextStyle _infoStyle = new TextStyle(Brushes.Green, null, FontStyle.Regular);
        //private static readonly TextStyle _errorStyle = new TextStyle(Brushes.Red, null, FontStyle.Regular);
        //private static readonly TextStyle _defaultStyle = new TextStyle(Brushes.White, null, FontStyle.Regular);


        public static int MaxLines = 500;

        public static void AttachStatusOutput(UIRichTextBox textBox)
        {
            _coloredStatusTextBox = textBox;
        }
        public static void AttachOperationOutput(UIRichTextBox textBox)
        {

            _coloredOperationTextBox = textBox;
        }

        /// <summary>
        /// 正常颜色显示
        /// </summary>
        /// <param name="formatString"></param>
        /// <param name="args"></param>
        public static void WriteStatusLine(string formatString, params object[] args)
        {
            var data = string.Format(formatString, args);

            _logger.Info(data);

            _coloredStatusTextBox?.BeginInvoke(new Action(() =>
            {
                AutoClear();

                _coloredStatusTextBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + data + Environment.NewLine);
                _coloredStatusTextBox.ScrollToCaret();

                if (_coloredStatusTextBox.Lines.Length > MaxLines)
                {
                    var rm = new List<int>();
                    for (int i = 0; i < MaxLines / 2; i++)
                    {
                        rm.Add(i);
                    }


                    _coloredStatusTextBox.ScrollToCaret();
                }

            }));
        }
        public static void WriteOperationLine(string formatString, params object[] args)
        {
            var data = string.Format(formatString, args);

            _logger.Info(data);

            _coloredOperationTextBox?.BeginInvoke(new Action(() =>
            {
                AutoClear();

                _coloredOperationTextBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + data + Environment.NewLine);


                if (_coloredOperationTextBox.Lines.Length > MaxLines)
                {
                    var rm = new List<int>();
                    for (int i = 0; i < MaxLines / 2; i++)
                    {
                        rm.Add(i);
                    }


                    _coloredOperationTextBox.ScrollToCaret();
                }

            }));
        }


        public static void WriteOperationLine(PresetColor color, string formatString, params object[] args)
        {
            try
            {
                var data = string.Format(formatString, args);
                _logger.Info(data);

                Color style;

                switch (color)
                {
                    case PresetColor.Yellow:
                        style = Color.Yellow;
                        break;
                    case PresetColor.Green:
                        style = Color.Green;
                        break;
                    case PresetColor.Red:
                        style = Color.Red;
                        break;
                    case PresetColor.White:
                        style = Color.White;
                        break;
                    case PresetColor.Black:
                        style = Color.Black;
                        break;
                    default:
                        style = Color.Black;
                        break;
                }
                _logger.Info(DateTime.Now.ToString("HH:mm:ss  ")+"Operation" + data + "\r\n");
                _coloredOperationTextBox?.BeginInvoke(new Action(() =>
                {
                    AutoClear();
                    _coloredOperationTextBox.SelectionColor = style;

                    _coloredOperationTextBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + data + "\r\n");
                    
                    _coloredOperationTextBox.ScrollToCaret();

                }));

            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
            }

        }

        public static void AutoClear()
        {
            int line = _coloredStatusTextBox.TextLength;
            //if (line >= 1024 * 1024 * 10)
            if (line >= 1000)//OutPut大于10000行,就清空
            {
                _coloredStatusTextBox.Clear();
                line = 0;
            }
            line = _coloredOperationTextBox.TextLength;
            if(line>=1000)
            {
                _coloredOperationTextBox.Clear();
                line = 0;

            }
        }

        /// <summary>
        /// 可变颜色显示
        /// </summary>
        /// <param name="color">red, yellow, white, green</param>
        /// <param name="formatString"></param>
        /// <param name="args"></param>
        public static void WriteStatusLine(PresetColor color, string formatString, params object[] args)
        {
            try
            {
                var data = string.Format(formatString, args);
                _logger.Info(data);

                Color style;

                switch (color)
                {
                    case PresetColor.Yellow:
                        style = Color.Yellow;
                        break;
                    case PresetColor.Green:
                        style = Color.Green;
                        break;
                    case PresetColor.Red:
                        style = Color.Red;
                        break;
                    case PresetColor.White:
                        style = Color.White;
                        break;
                    case PresetColor.Black:
                        style = Color.Black;
                        break;
                    default:
                        style = Color.Black;
                        break;
                }
                _logger.Info(DateTime.Now.ToString("HH:mm:ss  ")+"Status" + data + "\r\n");
                _coloredStatusTextBox?.BeginInvoke(new Action(() =>
                {
                    AutoClear();
                    _coloredStatusTextBox.SelectionColor = style;

                    _coloredStatusTextBox.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + data + "\r\n");
                    _coloredStatusTextBox.ScrollToCaret();

                }));

            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
            }

        }
    }
}
